﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Webcar.BLL;
using BusinessEntity;

namespace Webcar.Car
{
    public partial class EditGpsSummary : PageBase
    {
        string dept
        {
            get
            {
                return Request.QueryString["dept"];
            }
        }
        string ny
        {
            get
            {
                return Request.QueryString["ny"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack) return;
            DropDownListManager.BindDropDownDW(this.ddlDept);
            this.tbNy.Text = DateTime.Now.ToString("yyyy-MM");
            if (string.IsNullOrEmpty(dept)) return;
            this.LoadGpsSummary(dept,ny);
        }

        void LoadGpsSummary(string dept, string ny)
        {
            BS_GPS_SUMMARYEntity entity = new BS_GPS_SUMMARYEntity();
            entity.DEPT_CODE = dept;
            entity.NY = ny;
            entity.Retrieve();
            this.ddlDept.SelectedValue = entity.DEPT_CODE;
            this.tbNy.Text = entity.NY;
            this.tbTotal.Text = entity.TOTAL_GPS.ToString();
            this.tb_cs_car_num.Text = entity.CS_CAR_NUM.ToString();
            this.cs_num.Text = entity.CS_NUM.ToString();
            this.yj_car_num.Text = entity.YJ_CAR_NUM.ToString();
            this.cs_stop_num.Text = entity.CS_STOP_NUM.ToString();
            this.yj_stop_num.Text = entity.YJ_STOP_NUM.ToString();
            
            this.cf_num.Text = entity.CF_NUM.ToString();
            this.cf_zj_num.Text = entity.CF_ZJ_NUM.ToString();
            
            this.tbNote.Text = entity.NOTE.ToString();
            this.ddlDept.Enabled = false;
            this.tbNy.Enabled = false;

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                BS_GPS_SUMMARYEntity entity = new BS_GPS_SUMMARYEntity();
                
                entity.DEPT_CODE = this.ddlDept.SelectedValue;
                if (string.IsNullOrEmpty(entity.DEPT_CODE))
                {
                    this.lblError.Text = "请选择部门。";
                    return;
                }
                entity.NY = this.tbNy.Text;
                DateTime dt = DateTime.Now;
                if (DateTime.TryParse(this.tbNy.Text + "-01",out dt)==false)
                {
                    this.lblError.Text = "月份格式错误。";
                    return;
                }
                if (entity.Retrieve()&&string.IsNullOrEmpty(this.dept))
                {
                    this.lblError.Text = "该单位在本月已经存在记录，不能重复添加。";
                    return;
                }
                entity.TOTAL_GPS = int.Parse(this.tbTotal.Text);
                entity.CS_CAR_NUM = int.Parse(this.tb_cs_car_num.Text);
                entity.CS_NUM = int.Parse(this.cs_num.Text);
                entity.YJ_CAR_NUM = int.Parse(this.yj_car_num.Text);
                entity.CS_STOP_NUM = int.Parse(this.cs_stop_num.Text);
                entity.YJ_STOP_NUM = int.Parse(this.yj_stop_num.Text);

                entity.CF_NUM = int.Parse(this.cf_num.Text);
                entity.CF_ZJ_NUM = int.Parse(this.cf_zj_num.Text);

                entity.NOTE = this.tbNote.Text;
                entity.Save();
                //跳转到中间页面
                this.GotoSucessPage("保存成功。", this.GetReturnUrl());
            }
            catch (FormatException)
            {
                this.lblError.Text = "你还有项目没有录入或者录入的数字格式错误。";
            }
            catch (Exception ex)
            {
                this.lblError.Text = "保存失败:" + ex.Message;
            }

        }
    }
}
